package com.aspire.moss.portal.desktop;

import java.util.Map;
import java.util.Random;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.aspire.moss.portal.R;

/**
 * 工具类
 * 
 * @author Administrator
 * 
 */
public class Utils {

    private static final String TAG = "Utils";

    private static final String APPINFO_DATA = "APPINFO_DATA";

    /**
     * 弹出是否删除的提示框
     * 
     * @param mContext
     * @param controller
     * @param appInfo
     */
    public static void createDeleteDailog(final Context mContext, final WorkspaceController controller, final AppInfo appInfo) {
        new AlertDialog.Builder(mContext).setNegativeButton(mContext.getString(R.string.no), new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).setPositiveButton(mContext.getString(R.string.yes), new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                controller.removeAppView(appInfo, View.VISIBLE);
            }
        }).setTitle(mContext.getString(R.string.dialog_tip)).setMessage(mContext.getString(R.string.del_app_msg)).create().show();
    }

    /**
     * 把view转成bitmap
     * 
     * @param view
     * @return
     */
    public final static Bitmap getViewBitmap(View view) {
        view.clearFocus();
        view.setPressed(false);

        final boolean willNotCache = view.willNotCacheDrawing();
        view.setWillNotCacheDrawing(false);

        // Reset the drawing cache background color to fully transparent
        // for the duration of this operation
        final int color = view.getDrawingCacheBackgroundColor();
        view.setDrawingCacheBackgroundColor(0);

        if (color != 0) {
            view.destroyDrawingCache();
        }
        view.buildDrawingCache();
        final Bitmap cacheBitmap = view.getDrawingCache();
        if (cacheBitmap == null) {
            Log.d(TAG, "failed getViewBitmap(" + view + ")");
            return null;
        }

        final Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

        // Restore the view
        view.destroyDrawingCache();
        view.setWillNotCacheDrawing(willNotCache);
        view.setDrawingCacheBackgroundColor(color);

        return bitmap;
    }

    /**
     * 放大图片
     * 
     * @param bitmap
     * @param zoomInt
     * @return
     */
    public static Bitmap scaleBitmap(Bitmap bitmap, float zoomInt) {
        if (null == bitmap)
            return null;
        final Matrix matrix = new Matrix();
        matrix.preScale(zoomInt, zoomInt);
        final Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
        return newBitmap;
    }

    /**
     * 把图像变成黑白的
     * 
     * @param bitmap
     * @return
     */
    public static Bitmap bitmapToBlackAndWhite(Bitmap bitmap) {
        Bitmap reBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(reBitmap);
        Paint paint = new Paint();
        ColorMatrix matrix = new ColorMatrix();
        matrix.setSaturation(0);
        ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(matrix);
        paint.setColorFilter(colorFilter);
        canvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.save();
        return reBitmap;
    }

    /**
     * 调节图像的亮度
     * 
     * @param bitmap
     * @param brightness
     * @return
     */
    public static Bitmap bitmapBrightness(Bitmap bitmap, int brightness) {
        Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        ColorMatrix cMatrix = new ColorMatrix();
        cMatrix.set(new float[] { 1, 0, 0, 0, brightness, 0, 1, 0, 0, brightness,// 改变亮度
                0, 0, 1, 0, brightness, 0, 0, 0, 1, 0 });

        Paint paint = new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(cMatrix));

        Canvas canvas = new Canvas(bmp);
        // 在Canvas上绘制一个已经存在的Bitmap。这样，dstBitmap就和srcBitmap一摸一样了
        canvas.drawBitmap(bitmap, 0, 0, paint);
        return bmp;
    }

    /**
     * 取得屏幕信息
     * 
     * @param mContext
     * @return
     */
    public static DisplayMetrics getDisplayMetrics(Context mContext) {
        return mContext.getResources().getDisplayMetrics();
    }

    public static int getScreenWidth(Context mContext) {
        return getDisplayMetrics(mContext).widthPixels;
    }

    public static int getScreenHeight(Context mContext) {
        return getDisplayMetrics(mContext).heightPixels;
    }

    public static LayoutInflater getLayoutInflater(Context mContext) {
        return (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public static boolean isToLeft(int point) {
        return point > 0 ? true : false;
    }

    public static boolean isToRight(int point) {
        return point < 0 ? true : false;
    }

    /**
     * 保存数据
     * 
     * @param mContext
     * @param appInfo
     * 
     *            public static void saveOrUpdateAppInfo(Context mContext ,
     *            AppInfo appInfo){ final SharedPreferences preferences =
     *            mContext.getSharedPreferences(APPINFO_DATA ,
     *            Context.MODE_PRIVATE);
     *            preferences.edit().putString(appInfo.screen + "_" +
     *            appInfo.cellX + "*" + appInfo.cellY ,
     *            appInfo.appInfoToJson()).commit();
     * 
     *            }
     */
    /**
     * 删除数据
     * 
     * @param mContext
     * @param appInfo
     * 
     *            public static void deleteAppInfo(Context mContext , AppInfo
     *            appInfo){ final SharedPreferences preferences =
     *            mContext.getSharedPreferences(APPINFO_DATA ,
     *            Context.MODE_PRIVATE);
     *            preferences.edit().remove(appInfo.screen + "_" + appInfo.cellX
     *            + "*" + appInfo.cellY).commit(); }
     */
    /**
     * 加载数据
     * 
     * @param mContext
     * @return
     */
    public static Map<String, AppInfo> getAppInfo(Context mContext) {
        // final SharedPreferences preferences =
        // mContext.getSharedPreferences(APPINFO_DATA , Context.MODE_PRIVATE);
        // final Map<String, AppInfo> map = new HashMap<String, AppInfo>();
        // final Map<String, String> srcMap = (Map<String, String>)
        // preferences.getAll();
        // Set<String> setKey = srcMap.keySet();
        // String appInfoJson = null;
        // AppInfo appInfo = null;
        // for(String key : setKey){
        // appInfoJson = srcMap.get(key);
        // appInfo = AppInfo.jsonToAppInfo(appInfoJson);
        //
        // }
        return null;
    }

    public static String[] icons = { "icon/blue_blank.png", "icon/blue_blank.png", "icon/browser.png", "icon/cal.png", "icon/calc.png", "icon/chat_blank.png",
            "icon/clock.png", "icon/graph.png", "icon/ipod.png", "icon/mail.png", "icon/map.png", "icon/notes.png", "icon/phone.png", "icon/sms.png",
            "icon/wallpaper.png", "icon/weather.png" };

    public static String randomIcon() {
        final Random random = new Random();
        final int id = random.nextInt(icons.length);
        final String ids = icons[id];
        Log.d("Util", ids);
        return ids;
    }

}
